/*
 * TANServiceClientl.java
 * 
 * Client that opens the connection to the TANService.
 * 
 * Created: 14.11.2008
 * 
 * Copyright (C) 2008 Fraunhofer SCAI
 * 
 * All rights reserved
 * 
 * https://gforge.beingrid.eu/gf/project/scailm/
 * 
 */
package de.fraunhofer.scai.lm.tanservice.client;

import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.BlockTANList;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.BlockTANListResponse;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.CreateTANList;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.CreateTANListResponse;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.UnblockTANList;
import de.fraunhofer.scai.lm.tanservice.client.gen.TANServiceStub.UnblockTANListResponse;

import java.rmi.RemoteException;

/**
 * Implementation of the TANServiceClient. The TANServiceClient is responsible
 * for forwarding the requests to the TANServer and for collecting the replies
 * from the server.
 * 
 * @author Yona Raekow
 */
public class TANServiceClient {

	/**
	 * The service stub packs the elements that are sent over the network.
	 */
	private TANServiceStub serviceStub;

	/**
	 * Constructor initializes the service stub
	 * 
	 * @param serviceStub
	 *            Interface to the TAN web service
	 */
	public TANServiceClient(TANServiceStub serviceStub) {
		this.serviceStub = serviceStub;
	}

	/**
	 * This method calls the createTANList function on the server which results
	 * in the creation of a new TANList.
	 * 
	 * @param String
	 *            acName the account name for which a TAN list shall be created
	 * @param int
	 *            numberOfTANs how many TANs the TAN list shall contain
	 * @throws RemoteException
	 *             in case there is a problem with the connection between client
	 *             and server
	 */
	public boolean createTANList(String billingAccount, String password, String accountName, int numberOfTANs)
			throws RemoteException {
		CreateTANList tanListRequest = new CreateTANList();
		tanListRequest.setBillingAccount(billingAccount);
		tanListRequest.setPassword(password);
		tanListRequest.setAccountName(accountName);
		tanListRequest.setNumberOfTANs(numberOfTANs);
		CreateTANListResponse response = serviceStub.createTANList(tanListRequest);
		return response.getSuccess();
	}

	/**
	 * This method calls the blockTANList function on the server which results
	 * in making the TANList temporarily unavailable.
	 * 
	 * @param acName
	 *            the account name for which the TANList shall be blocked
	 * @return
	 * @throws RemoteException
	 *             in case there is a problem with the connection between client
	 *             and server
	 */
	public boolean blockTANList(String billingAccount, String password,String accountName) throws RemoteException {
		BlockTANList request = new BlockTANList();
		request.setAccountName(accountName);
		request.setBillingAccount(billingAccount);
		request.setPassword(password);
		BlockTANListResponse response = serviceStub.blockTANList(request);
		return response.getSuccess();
	}

	/**
	 * This method calls the unBlockTANList function on the server which results
	 * in making a blocked TANList usable again.
	 * 
	 * @param acName
	 *            the account name for which the TANList shall be unblocked
	 * @throws RemoteException
	 *             in case there is a problem with the connection between client
	 *             and server
	 */
	public boolean unBlockTANList(String billingAccount, String password,String accountName) throws RemoteException {
		UnblockTANList request = new UnblockTANList();
		request.setAccountName(accountName);
		request.setBillingAccount(billingAccount);
		request.setPassword(password);
		UnblockTANListResponse response = serviceStub.unblockTANList(request);
		return response.getSuccess();
	}
}
